package dobest.ray.com.jiongjiong.views.chart;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;

import java.util.Collections;
import java.util.List;

import dobest.ray.com.jiongjiong.R;


public class EyeChartView extends View {
	private Context context;
	private List<Float> PointLeft;// 左眼数据
	private List<Float> PointRight;// 右眼数据
	private List<String> dateArray;// 右眼数据
	private List<Float> PointDanger;

	private int itemLength = 0;// 横坐标没格刻度长度

	private int maxSize = 0;// 数据个数

	private Paint paintTime;// 时间画笔
	private Paint paintLineLeft;// 左眼折线画笔
	private Paint paintLineDanger;// 左眼折线画笔
	private Paint paintLineRight;// 右眼折线画笔
	private Paint paintLineTwoData;// 左右眼折线画笔
	private Paint paintCircleLeft;// 左眼圆画笔
	private Paint paintCircleRight;// 右眼圆画笔
	private Paint paintDrawText;// 字体圆画笔
	private Paint framPanint;// 阴影画笔
	private float paddingBottom = 50.0f;
	private float paddingLeft = 35.0f;
	private float paddingTop = 10.0f;

	private int radusNormalCircle = 10;// 平常圆半径
	private int radusNormalCircleR = 13;// 平常圆半径
	private int radusBigCircle = 20;// 大圆半径

	private boolean isFinish = false;// 是否绘制完成

	private Bitmap bitmap_point;
	private Bitmap bitmap_left_small;
	private Bitmap bitmap_left;
	private Bitmap bitmap_right;
	private Bitmap bitmap_right_small;
	private float min = -1;
	private float max = -1;
	private float base;// 每一格代表多少像素

	public EyeChartView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.context = context;
		init();
	}

	public EyeChartView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public EyeChartView(Context context, List<Float> PointLeft,
			List<Float> PointRight, List<Float> PointDanger,
			List<String> dateArray, float max, float min) {
		super(context);
		this.context = context;
		this.PointLeft = PointLeft;
		this.PointRight = PointRight;
		this.dateArray = dateArray;
		this.PointDanger = PointDanger;
		this.max = max;
		this.min = min;
		if (PointLeft == null || PointRight == null || dateArray == null) {
			return;
		}
		if (PointRight.size() > PointLeft.size()) {
			maxSize = PointRight.size();
		} else {
			maxSize = PointLeft.size();
		}
		init();
	}

	public EyeChartView(Context context, List<Float> PointLeft,
			List<Float> PointRight, List<Float> PointDanger,
			List<String> dateArray, float max, float min, boolean isShowInt) {
		super(context);
		this.context = context;
		this.PointLeft = PointLeft;
		this.PointRight = PointRight;
		this.dateArray = dateArray;
		this.PointDanger = PointDanger;
		this.max = max;
		this.min = min;
		this.isShowInt = isShowInt;
		if (PointLeft == null || PointRight == null || dateArray == null) {
			return;
		}
		if (PointRight.size() > PointLeft.size()) {
			maxSize = PointRight.size();
		} else {
			maxSize = PointLeft.size();
		}
		init();
	}

	private void init() {
		paintTime = new Paint();
		paintTime.setStrokeWidth(2f);
		paintTime.setTextSize(18);
		paintTime.setColor(getResources().getColor(R.color.white));
		paintTime.setAntiAlias(true);

		paintLineLeft = new Paint();
		paintLineLeft.setStrokeWidth(4f);
		paintLineLeft.setColor(getResources().getColor(R.color.white));
		paintLineLeft.setAntiAlias(true);

		paintLineDanger = new Paint();
		paintLineDanger.setStrokeWidth(3f);
		paintLineDanger.setColor(getResources().getColor(R.color.peachpuff));
		paintLineDanger.setAntiAlias(true);

		paintLineRight = new Paint();
		paintLineRight.setStrokeWidth(4f);
		paintLineRight.setColor(getResources().getColor(R.color.white));
		paintLineRight.setAntiAlias(true);

		paintLineTwoData = new Paint();
		paintLineTwoData.setStrokeWidth(1f);
		paintLineTwoData.setColor(getResources().getColor(R.color.white));
		paintLineTwoData.setAntiAlias(true);

		paintDrawText = new Paint();
		paintDrawText.setStrokeWidth(3f);
		paintDrawText.setTextSize(32);
		paintDrawText.setColor(getResources().getColor(R.color.lightsalmon));
		paintDrawText.setAntiAlias(true);

		paintCircleLeft = new Paint();
		paintCircleLeft.setStyle(Paint.Style.FILL_AND_STROKE);
		paintCircleLeft.setStrokeWidth(6f);
		paintCircleLeft.setColor(getResources().getColor(R.color.lightsalmon));
		paintCircleLeft.setAntiAlias(true);

		paintCircleRight = new Paint();
		paintCircleRight.setStyle(Paint.Style.FILL_AND_STROKE);
		paintCircleRight.setStrokeWidth(6f);
		paintCircleRight.setColor(getResources().getColor(R.color.fuchsia));
		paintCircleRight.setAntiAlias(true);

		framPanint = new Paint();
		framPanint.setAntiAlias(true);
		framPanint.setStrokeWidth(2f);

		itemLength = (getScreenWidth() - 50) / 6;

		bitmap_point = BitmapFactory.decodeResource(getResources(),
				R.mipmap.img_baidu_marker_bg);
		bitmap_right = BitmapFactory.decodeResource(getResources(),
				R.mipmap.img_right_point);
		bitmap_left = BitmapFactory.decodeResource(getResources(),
				R.mipmap.img_left_point);
		bitmap_right_small = BitmapFactory.decodeResource(getResources(),
				R.mipmap.img_right_point_small);
		bitmap_left_small = BitmapFactory.decodeResource(getResources(),
				R.mipmap.img_left_point_small);
		if (max == 0 && min == 0) {
			if (Collections.max(PointLeft) > Collections.max(PointRight)) {
				max = Collections.max(PointLeft);
			} else {
				max = Collections.max(PointRight);
			}
			if (Collections.min(PointLeft) > Collections.min(PointRight)) {
				min = Collections.min(PointRight);
			} else {
				min = Collections.min(PointLeft);
			}
			if (max < Collections.max(PointDanger)) {
				max = Collections.max(PointDanger);
			}
			if (min > Collections.min(PointDanger)) {
				min = Collections.min(PointDanger);
			}
			if (maxSize == 1) {
				if (min > 0) {
					min *= 0.8;
				} else {
					min *= 1.2;
				}
			}
		}

		clickIndex = maxSize - 1;
		clickIndexR = maxSize - 1;
		setLayoutParams(new LayoutParams(
				maxSize * itemLength < getScreenWidth() ? 4 * itemLength
						: maxSize * itemLength, LayoutParams.MATCH_PARENT));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		base = (getHeight() - paddingBottom - paddingTop) / (max - min) / 1.1f;// 每一格刻度代表多高
		// canvas.drawText("100", 30, 10 * 1.0f, paintTime);
		drawDangerArea(canvas);
		drawTime(canvas);
		drawData(canvas);
		super.onDraw(canvas);
	}

	// 绘制危险区域
	private void drawDangerArea(Canvas canvas) {
		Shader mShader = new LinearGradient(0, 0, 0, getHeight(), new int[] {
				Color.argb(100, 255, 0, 0), Color.argb(45, 255, 0, 0),
				Color.argb(45, 255, 0, 0) }, null, Shader.TileMode.CLAMP);
		framPanint.setShader(mShader);

		Path path = new Path();
		if (maxSize == 1) {
			path.lineTo(paddingLeft + itemLength * 1, getHeight());
			path.lineTo(paddingLeft + itemLength * 1, getHeight()
					- (PointDanger.get(0) - min) * base - paddingBottom);
			path.lineTo(paddingLeft + itemLength * 2, getHeight()
					- (PointDanger.get(0) - min) * base - paddingBottom);
			path.lineTo(paddingLeft + itemLength * 3, getHeight()
					- (PointDanger.get(0) - min) * base - paddingBottom);
			path.lineTo(paddingLeft + itemLength * 3, getHeight());
			path.lineTo(paddingLeft + itemLength * 1, getHeight());
			path.close();
			canvas.drawPath(path, framPanint);

		} else {
			for (int i = 0; i < PointDanger.size() - 1; i++) {
				float startX = paddingLeft + itemLength * i;
				float stopX = paddingLeft + itemLength * (i + 1);
				float startY = (getHeight() - paddingBottom)
						- (PointDanger.get(i) - min) * base;
				float stopY = (getHeight() - paddingBottom)
						- (PointDanger.get(i + 1) - min) * base;
				if (i == 0) {
					path.lineTo(startX, getHeight() - paddingBottom);
				}
				path.lineTo(startX, startY);
				if (i == PointDanger.size() - 2) {
					path.lineTo(stopX, stopY);
					path.lineTo(stopX, getHeight() - paddingBottom);
					path.lineTo(paddingLeft, getHeight() - paddingBottom);
					path.close();
					canvas.drawPath(path, framPanint);
				}
				canvas.drawLine(startX, startY, stopX, stopY, paintLineDanger);
			}
		}
	}

	// 绘制图片点
	private void drawData(Canvas canvas) {
		if (maxSize == 1) {
			// 当只有一个点得时候
			if (PointLeft.size() == 1) {
				if (isLeft) {
					if (clickIndex == 0) {
						// canvas.drawCircle(20 + itemLength * 2,
						// (getHeight() - 20) - (PointLeft.get(0) - min)
						// * base, radusBigCircle, paintCircleLeft);

						canvas.drawBitmap(
								bitmap_left,
								paddingLeft + itemLength * 2
										- bitmap_left.getWidth() / 2,
								(getHeight() - paddingBottom)
										- (PointLeft.get(0) - min) * base
										- bitmap_left.getHeight() / 2, null);

						drawShowText(
								canvas,
								paddingLeft + itemLength * 2,
								(getHeight() - paddingBottom)
										- (PointLeft.get(0) - min) * base,
								PointLeft.get(0));
					} else {

						canvas.drawBitmap(
								bitmap_left_small,
								paddingLeft + itemLength * 2
										- bitmap_left_small.getWidth() / 2,
								(getHeight() - paddingBottom)
										- (PointLeft.get(0) - min) * base
										- bitmap_left_small.getHeight() / 2,
								null);

						// canvas.drawCircle(20 + itemLength * 2,
						// (getHeight() - 20) - (PointLeft.get(0) - min)
						// * base, radusNormalCircle,
						// paintCircleLeft);
					}
				} else {
					// 左点
					canvas.drawBitmap(
							bitmap_left_small,
							paddingLeft + itemLength * 2
									- bitmap_left_small.getWidth() / 2,
							(getHeight() - paddingBottom)
									- (PointLeft.get(0) - min) * base
									- bitmap_left_small.getHeight() / 2, null);
					// canvas.drawCircle(20 + itemLength * 2, (getHeight() - 20)
					// - (PointLeft.get(0) - min) * base,
					// radusNormalCircle, paintCircleLeft);
				}
			}
			if (PointRight.size() == 1) {
				if (isRight) {
					if (clickIndexR == 0) {
						canvas.drawBitmap(
								bitmap_right,
								paddingLeft + itemLength * 2
										- bitmap_right.getWidth() / 2,
								(getHeight() - paddingBottom)
										- (PointRight.get(0) - min) * base
										- bitmap_right.getHeight() / 2, null);

						// canvas.drawCircle(20 + itemLength * 2,
						// (getHeight() - 20) - (PointRight.get(0) - min)
						// * base, radusBigCircle,
						// paintCircleRight);
						drawShowText(
								canvas,
								paddingLeft + itemLength * 2,
								(getHeight() - paddingBottom)
										- (PointRight.get(0) - min) * base,
								PointRight.get(0));
					} else {

						canvas.drawBitmap(
								bitmap_right_small,
								paddingLeft + itemLength * 2
										- bitmap_right_small.getWidth() / 2,
								(getHeight() - paddingBottom)
										- (PointRight.get(0) - min) * base
										- bitmap_right_small.getHeight() / 2,
								null);
						// 右点
						// canvas.drawCircle(20 + itemLength * 2,
						// (getHeight() - 20) - (PointRight.get(0) - min)
						// * base, radusNormalCircleR,
						// paintCircleRight);
					}
				} else {

					canvas.drawBitmap(bitmap_right_small, paddingLeft
							+ itemLength * 2 - bitmap_right_small.getWidth()
							/ 2,
							(getHeight() - paddingBottom)
									- (PointRight.get(0) - min) * base
									- bitmap_right_small.getHeight() / 2, null);
					// 右点
					// canvas.drawCircle(20 + itemLength * 2, (getHeight() - 20)
					// - (PointRight.get(0) - min) * base,
					// radusNormalCircleR, paintCircleRight);
				}

			}
		} else {

			for (int i = 0; i < maxSize - 1; i++) {
				float startX = paddingLeft + itemLength * i;
				float stopX = paddingLeft + itemLength * (i + 1);
				float startLY = (getHeight() - paddingBottom)
						- (PointLeft.get(i) - min) * base;
				float stopLY = (getHeight() - paddingBottom)
						- (PointLeft.get(i + 1) - min) * base;
				float startRY = (getHeight() - paddingBottom)
						- (PointRight.get(i) - min) * base;
				float stopRY = (getHeight() - paddingBottom)
						- (PointRight.get(i + 1) - min) * base;
				canvas.drawLine(startX, startLY, stopX, stopLY, paintLineLeft);

				// 同X轴两点连线
				canvas.drawLine(startX, startLY, startX, startRY,
						paintLineTwoData);

				canvas.drawLine(startX, startRY, stopX, stopRY, paintLineRight);

				// 右点
				// canvas.drawCircle(startX, startRY, radusNormalCircleR,
				// paintCircleRight);

				canvas.drawBitmap(bitmap_right_small, startX
						- bitmap_right_small.getWidth() / 2, startRY
						- bitmap_right_small.getHeight() / 2, null);

				canvas.drawBitmap(bitmap_left_small,
						startX - bitmap_left_small.getWidth() / 2, startLY
								- bitmap_left_small.getHeight() / 2, null);

				// 左点
				// canvas.drawCircle(startX, startLY, radusNormalCircle,
				// paintCircleLeft);

				// 最后一个点
				if (i == maxSize - 2) {
					canvas.drawLine(stopX, stopLY, stopX, stopRY,
							paintLineTwoData);
					// canvas.drawCircle(startX, startLY, radusNormalCircle,
					// paintCircleLeft);
					// canvas.drawCircle(stopX, stopRY, radusNormalCircleR,
					// paintCircleRight);

					canvas.drawBitmap(bitmap_right_small, startX
							- bitmap_right_small.getWidth() / 2, startRY
							- bitmap_right_small.getHeight() / 2, null);
					canvas.drawBitmap(bitmap_right_small, stopX
							- bitmap_right_small.getWidth() / 2, stopRY
							- bitmap_right_small.getHeight() / 2, null);

					canvas.drawBitmap(bitmap_left_small, startX
							- bitmap_left_small.getWidth() / 2, startLY
							- bitmap_left_small.getHeight() / 2, null);
					canvas.drawBitmap(bitmap_left_small, stopX
							- bitmap_left_small.getWidth() / 2, stopLY
							- bitmap_left_small.getHeight() / 2, null);
					// canvas.drawCircle(stopX, stopLY, radusNormalCircle,
					// paintCircleLeft);
				}
				// 画点
				if (isRight) {
					if (clickIndexR == i) {
						canvas.drawCircle(startX, startRY, radusBigCircle,
								paintCircleRight);

						canvas.drawBitmap(bitmap_right,
								startX - bitmap_right.getWidth() / 2, startRY
										- bitmap_right.getHeight() / 2, null);

						drawShowText(canvas, startX, startRY, PointRight.get(i));
					} else if (i == maxSize - 2 && clickIndexR == maxSize - 1) {
						// canvas.drawCircle(stopX, stopRY, radusBigCircle,
						// paintCircleRight);

						canvas.drawBitmap(bitmap_right,
								stopX - bitmap_right.getWidth() / 2, stopRY
										- bitmap_right.getHeight() / 2, null);
						drawShowText(canvas, stopX, stopRY,
								PointRight.get(clickIndexR));
					}
				}
				// 画点
				if (isLeft) {
					if (clickIndex == i) {
						// canvas.drawCircle(startX, startLY, radusBigCircle,
						// paintCircleLeft);
						canvas.drawBitmap(bitmap_left,
								startX - bitmap_left.getWidth() / 2, startLY
										- bitmap_left.getHeight() / 2, null);

						drawShowText(canvas, startX, startLY, PointLeft.get(i));
					} else if (i == maxSize - 2 && clickIndex == maxSize - 1) {
						// canvas.drawCircle(stopX, stopLY, radusBigCircle,
						// paintCircleLeft);
						canvas.drawBitmap(bitmap_left,
								stopX - bitmap_left.getWidth() / 2, stopLY
										- bitmap_left.getHeight() / 2, null);
						drawShowText(canvas, stopX, stopLY,
								PointLeft.get(clickIndex));
					}
				}

			}
		}
		isFinish = true;

	}

	private boolean isShowInt = false;

	// 绘制弹出框背景字
	private void drawShowText(Canvas canvas, float startX, float startY,
			float value) {
		canvas.drawBitmap(bitmap_point, startX - bitmap_point.getWidth() / 2,
				startY - bitmap_point.getHeight() - 16, null);
		canvas.drawText(
				isShowInt ? (int) value + "" : value + "",
				startX
						- paintDrawText.measureText(isShowInt ? (int) value
								+ "" : value + "") / 2,
				startY - bitmap_point.getHeight() - 16
						+ (bitmap_point.getHeight() / 2 + 8), paintDrawText);
	}

	// 绘制日期
	private void drawTime(Canvas canvas) {

		for (int i = 0; i < dateArray.size() && i < maxSize; i++) {
			if (i == 0) {
				if (maxSize == 1) {
					canvas.drawText(dateArray.get(i), 2 * itemLength,
							getHeight(), paintTime);
				} else {
					canvas.drawText(dateArray.get(i), 0, getHeight(), paintTime);
				}
			} else {
				canvas.drawText(dateArray.get(i), i * itemLength, getHeight(),
						paintTime);
			}
		}
	}

	private int getScreenWidth() {
		WindowManager wm = (WindowManager) getContext().getSystemService(
				Context.WINDOW_SERVICE);
		return wm.getDefaultDisplay().getWidth();
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			return true;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			int X = (int) event.getX();
			int Y = (int) event.getY();
			judgeXY(X, Y);
			break;
		}
		return false;
	}

	private boolean isLeft = true;
	private boolean isRight = true;
	private int clickIndex = -1;
	private int clickIndexR = -1;

	/**
	 * 判断点击的位置 是否是在点上
	 * 
	 * @param x
	 * @param y
	 */
	private void judgeXY(int x, int y) {

		if (!isFinish) {
			return;
		}
		for (int i = 0; i < maxSize; i++) {
			int temX = (int) (maxSize == 1 ? paddingLeft + itemLength * 2
					: paddingLeft + itemLength * i);
			// 判断左眼
			int temLY = (int) ((getHeight() - paddingBottom) - (PointLeft
					.get(i) - min) * base);
			int temL = (temX - x) * (temX - x) + (temLY - y) * (temLY - y);
			if (temL < 30 * 30) {
				clickIndex = i;
				isLeft = true;
				isRight = false;
				invalidate();
				break;
			} else {
				clickIndex = -1;
				clickIndexR = -1;
			}
			// 判断右眼
			int temRY = (int) ((getHeight() - paddingBottom) - (PointRight
					.get(i) - min) * base);
			int temR = (temX - x) * (temX - x) + (temRY - y) * (temRY - y);
			if (temR < 30 * 30) {
				clickIndexR = i;
				isRight = true;
				isLeft = false;
				invalidate();
				break;
			} else {
				clickIndex = -1;
				clickIndexR = -1;
			}

		}
	}
}
